package com.example.redisdemo;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import com.example.entity.UserVo;
import com.example.util.RedisKeyUtil;

@Service
public class RdsServiceTest {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private ValueOperations<String,Object> valueOperations;

    @Autowired
    private HashOperations<String, String, Object> hashOperations;

    @Autowired
    private ListOperations<String, Object> listOperations;

    @Autowired
    private SetOperations<String, Object> setOperations;

    @Autowired
    private ZSetOperations<String, Object> zSetOperations;

	
    /**
     * 使用value类型存储对象
     * @throws Exception
     */
    public void testObj() throws Exception{
        UserVo userVo = new UserVo();
        userVo.setAddress("上海");
        userVo.setName("测试dfas");
        userVo.setAge(123);
        String key = RedisKeyUtil.getKey(UserVo.Table,"name",userVo.getName());
        valueOperations.set(key, userVo, 20, TimeUnit.MINUTES);
        UserVo vo = (UserVo) valueOperations.get(key);
        System.out.println(vo.toString());
    }
    
    public void testStringKey() {
    	
		Map<String, String> valueMap = new HashMap<String, String>(); 
		valueMap.put("valueMap1","map1"); 
		valueMap.put("valueMap2","map2"); 
		valueMap.put("valueMap3","map3"); 
		valueOperations.multiSet(valueMap);
//    	判断是否有key所对应的值，有则返回true，没有则返回false
		Boolean hasKey = redisTemplate.hasKey("valueMap1");
		System.out.println(hasKey);
    }
    /**
     * set类型存取
     * @throws Exception
     */
    public void testSetOperation() throws Exception{
        UserVo userVo = new UserVo();
        userVo.setAddress("北京");
        userVo.setName("jantent");
        userVo.setAge(23);
        UserVo auserVo = new UserVo();
        auserVo.setAddress("n柜昂周");
        auserVo.setName("antent");
        auserVo.setAge(23);
        setOperations.add("user:test",userVo,auserVo);
        Set<Object> result = setOperations.members("user:test");
        System.out.println(result);
    }
    /**
     * hash类型存取
     * @throws Exception
     */
    public void HashOperations() throws Exception{
        UserVo userVo = new UserVo();
        userVo.setAddress("北京");
        userVo.setName("jantent");
        userVo.setAge(23);
        hashOperations.put("hash:user",userVo.hashCode()+"",userVo);
        System.out.println(hashOperations.get("hash:user",userVo.hashCode()+""));
        /**
         * 使用map一次存入多个
         */
		Map<String, String> valueMap = new HashMap<String, String>(); 
		valueMap.put("valueMap1","map1"); 
		valueMap.put("valueMap2","map2"); 
		valueMap.put("valueMap3","map3"); 
		hashOperations.putAll("hashStringkey", valueMap);
		System.out.println(hashOperations.get("hashStringkey", "valueMap2"));
    }
    /**
     * list类型，有序，从左侧开始放入，letPop弹出左侧一个并删除；rightPop弹出右侧一个并删除
     * @throws Exception
     */
    public void  ListOperations() throws Exception{
        UserVo userVo = new UserVo();
        userVo.setAddress("北京1");
        userVo.setName("ListOperations1");
        userVo.setAge(23);
        UserVo userVo2 = new UserVo();
        userVo2.setAddress("北京2");
        userVo2.setName("ListOperations2");
        userVo2.setAge(25);
        listOperations.leftPush("list:user",userVo);
        listOperations.leftPush("list:user",userVo2);
//        // pop之后 值会消失
//        System.out.println(listOperations.leftPop("list:user"));
        // pop之后 值会消失
        System.out.println(listOperations.rightPop("list:user"));
    }

}
